package com.smartcruise.main.mainFragment;

import android.util.Log;

import com.smartcruise.base.RefreshContract;
import com.smartcruise.data.PushData;
import com.smartcruise.data.cruiseData.CruiseContract;
import com.smartcruise.data.cruiseData.CruiseData;
import com.smartcruise.task.TaskAdapter;
import com.smartcruise.util.Key;
import com.smartcruise.util.ListUtils;
import com.smartcruise.util.TempleUser;
import com.smartcruise.util.TimeUtils;
import com.smartcruise.util.network.callBack.SimpleCallBack;
import com.smartcruise.util.network.netUtils.NetCollection;
import com.smartcruise.util.network.netUtils.Result;

import java.util.Calendar;

import io.realm.RealmChangeListener;
import io.realm.RealmResults;

/**
 * Created by FengChaoQun
 * on 2018/1/19 0019
 */

public class MainPresenterImp implements RefreshContract.Presenter {

    private MainContract.View mView;
    private RealmResults<CruiseData> newTaskList;
    private RealmResults<CruiseData> completedTaskList;
    private RealmResults<PushData> pushDataRealmResults;
    private RefreshContract.RefreshInterface refreshInterface;

    public MainPresenterImp(MainContract.View mView) {
        this.mView = mView;
        refreshInterface = mView.getRefresh();
    }

    @Override
    public void start() {
        RealmResults<CruiseData> all = mView.getRealm().where(CruiseData.class)
                .equalTo(Key.inspectorId, TempleUser.getInstance().getAccount())
                .beginGroup()
                .beginGroup()//找出本地的已开始的任务
                .equalTo(Key.isCopyData, true)
                .not()
                .equalTo(Key.statusInApp, CruiseContract.TASK_IS_WAIT_START)
                .endGroup()
                .or()
                .beginGroup()//找出已上传的但是还没有审核通过的任务
                .equalTo(Key.isFromNet, true)
                .not()
                .equalTo(Key.auditResult, CruiseContract.AUDIT_SUCCESS)
                .endGroup()
                .endGroup()
                .findAll();
        TaskAdapter adapter = new TaskAdapter(mView.getActivity(), all);
        mView.setData(adapter);

        initNewTaskNotice();
        initNewPushNotice();
        initCompletedPushNotice();

        //首页没有分页加载 所以没有下一页数据
        refreshInterface.setNoMoreData(true);

//        NetCollection.getFailedTaskBack(new SimpleCallBack(mView) {
//            @Override
//            public void onSuccess(Result result) {
//                Log.d("MainPresenterImp", "获取被驳回任务成功");
//            }
//        });
        refreshInterface.autoRefresh();
    }

    /**
     * description:初始化新任务提示红点
     */

    private void initNewTaskNotice() {
        Calendar calendar = Calendar.getInstance();
        //查询当天所有的需要执行的本地任务
        newTaskList = mView.getRealm().where(CruiseData.class)
                .equalTo(Key.inspectorId, TempleUser.getInstance().getAccount())
                .equalTo(Key.isCopyData, true)
                .equalTo(Key.statusInApp, CruiseContract.TASK_IS_WAIT_START)
                .between(Key.shouldRunDate, TimeUtils.getDayBeginTime(calendar).getTime(), TimeUtils.getDayEndTime(calendar).getTime())
                .findAll();
        mView.showNewTaskNoticeCount(newTaskList.size());
        newTaskList.addChangeListener(new RealmChangeListener<RealmResults<CruiseData>>() {
            @Override
            public void onChange(RealmResults<CruiseData> cruiseData) {
                mView.showNewTaskNoticeCount(newTaskList.size());
            }
        });
    }

    /**
     * description:初始化新消息提醒红点
     */

    private void initNewPushNotice() {
        pushDataRealmResults = mView.getRealm().where(PushData.class)
                .equalTo(Key.userId, TempleUser.getInstance().getAccount())
                .equalTo("unRead", true)
                .findAll();
        if (!ListUtils.isHaveContent(pushDataRealmResults)) {
            mView.showNoticeCount(null);
        } else {
            mView.showNoticeCount(String.valueOf(pushDataRealmResults.size()));
        }
        pushDataRealmResults.addChangeListener(new RealmChangeListener<RealmResults<PushData>>() {
            @Override
            public void onChange(RealmResults<PushData> pushData) {
                if (!ListUtils.isHaveContent(pushDataRealmResults)) {
                    mView.showNoticeCount(null);
                } else {
                    mView.showNoticeCount(String.valueOf(pushDataRealmResults.size()));
                }
            }
        });
    }

    /**
     * description:初始化已完成消息提醒红点
     */

    private void initCompletedPushNotice() {
        completedTaskList = mView.getRealm().where(CruiseData.class)
                .equalTo(Key.inspectorId, TempleUser.getInstance().getAccount())
                .equalTo(Key.auditStatus, CruiseContract.AUDITED)
                .equalTo(Key.auditResult, CruiseContract.AUDIT_SUCCESS)
                .findAll();
        mView.showCompletedTaskNoticeCount(completedTaskList.size());
        completedTaskList.addChangeListener(new RealmChangeListener<RealmResults<CruiseData>>() {
            @Override
            public void onChange(RealmResults<CruiseData> cruiseData) {
                mView.showCompletedTaskNoticeCount(completedTaskList.size());
            }
        });
    }

    /**
     * description:下拉刷新
     */
    @Override
    public void refresh() {
        NetCollection.getAllTask(mView, new SimpleCallBack(mView) {
            @Override
            public void onSuccess(Result result) {
                mView.showToast("刷新数据成功");
            }
        });
        NetCollection.getFailedTaskBack(new SimpleCallBack(mView) {
            @Override
            public void onSuccess(Result result) {
                Log.d("MainPresenterImp", "获取被驳回任务成功");
            }
        });
    }

    /**
     * description:上拉加载
     */
    @Override
    public void loadMore() {

    }

    @Override
    public void destroy() {
        if (newTaskList != null) {
            newTaskList.removeAllChangeListeners();
        }
        if (pushDataRealmResults != null) {
            pushDataRealmResults.removeAllChangeListeners();
        }
        if (completedTaskList != null) {
            completedTaskList.removeAllChangeListeners();
        }
    }

}
